<script>
	/* eslint-disable */
	import Vue from 'vue';
	import {
		configList,
		bindingEquipment
	} from '@/api/basic';
	import {
		verifyAccessToken
	} from '@/api/login'
	import {
		mapMutations
	} from 'vuex';
	export default {
		async onLaunch() {
			uni.setTabBarStyle({
				selectedColor: this.themeColor.color,
				borderStyle: 'white'
			});
			this.themeColor.tabList && this.themeColor.tabList.forEach((selectedIconPath, index) => {
				uni.setTabBarItem({
					index,
					selectedIconPath
				});
			});
			await this.initData();
		},
		methods: {
			...mapMutations(['setCartNum', 'setNotifyNum']),
			// 数据初始化
			async initData() {
				// 获取页面设置配置
				await this.initConfigList();
				// 获取页面设置配置
				const token = uni.getStorageSync('accessToken');
				// 获取系统title高度
				await this.initSystemInfo();
				if (token) {
					//await this.handleVerifyAccessToken(token);
				}
				if (this.$mStore.getters.hasLogin) {
					// 初始化购物车数量
					//this.setCartNum(uni.getStorageSync('cartNum') || 0);
					this.setNotifyNum(uni.getStorageSync('notifyNum') || 0);
					// #ifdef APP-PLUS
					const info = plus.push.getClientInfo();
					// #endif
				}
				// #ifdef H5
				await this.$mPayment.wxConfigH5(window.location.href);
				// #endif
				this.jxb.getRealNearestRegion();
			},
			// 初始化系统信息
			initSystemInfo() {
				uni.getSystemInfo({
					success(e) {
						// #ifdef APP-PLUS
						Vue.prototype.StatusBar = e.statusBarHeight;
						if (e.platform === 'android') {
							Vue.prototype.CustomBar = e.statusBarHeight + 50;
						} else {
							Vue.prototype.CustomBar = e.statusBarHeight + 43;
						}
						// #endif
						// #ifdef MP-WEIXIN
						Vue.prototype.StatusBar = e.statusBarHeight;
						// eslint-disable-next-line
						const custom = wx.getMenuButtonBoundingClientRect();
						Vue.prototype.Custom = custom;
						Vue.prototype.CustomBar = custom.top - e.statusBarHeight;
						// #endif
						// #ifdef MP-ALIPAY
						Vue.prototype.StatusBar = e.statusBarHeight;
						Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
						// #endif
					}
				});
			},
			async initConfigList() {
				const globalConfig = this.$mStore.getters.globalConfig;
				if (globalConfig) {
					this.initSettingConfig(globalConfig)
				}
				await this.$http
					.get(configList, {
						field:
							'app_name,app_logo,app_city'
					})
					.then(async r => {
						
						if (!this.$mHelper.isObjectValueEqual(this.$mStore.getters.globalConfig, r)) {
							this.initSettingConfig(r)
							await this.$mStore.dispatch('globalConfigChange', r);
						}
					});
			},
			// 初始化全局配置
			initSettingConfig(data) {
				console.log(data);
				this.$mSettingConfig['appName'] = data['app_name'] || this.$mSettingConfig['appName'];
				this.$mSettingConfig['appLogo'] = data['app_logo'] || this.$mSettingConfig['appLogo'];
				this.$mSettingConfig['appcity'] = data['app_city'] || this.$mSettingConfig['appcity'];
				this.$mSettingConfig['appTelephone'] = data['app_telephone'] ;
				this.$mSettingConfig['apphdomain'] = data['app_hdomain'];
				this.$mSettingConfig['apphouse_poster_bg'] = data['app_house_poster_bg'];
				
				if (!this.$mSettingConfig['styleUserIsOpen']) {
					this.$mConstDataConfig.themeList.forEach(item => {
						if (item.name === this.$mSettingConfig['styleType']) {
							this.$mStore.commit('setThemeColor', item);
						}
					});
				}
				uni.setTabBarStyle({
					selectedColor: this.themeColor.color,
					borderStyle: 'white'
				});
				this.themeColor.tabList && this.themeColor.tabList.forEach((selectedIconPath, index) => {
					uni.setTabBarItem({
						index,
						width: 20,
						selectedIconPath
					});
				});
			},
			// 检验token是否有效
			async handleVerifyAccessToken(token) {
				await this.$http.post(verifyAccessToken, {
					token
				}).then(r => {
					if (!r) { //TODO
						// this.$mStore.commit('logout');
					}
				});
			}
		}
	};
</script>
<style lang="scss">
	// 导入colorUI
	@import '/static/css/colorui/main.css';
	@import '/static/css/colorui/icon.css';
	@import '/static/css/colorui/animation.css';
	// 导入阿里巴巴矢量图标库
	/*#ifdef MP*/
	@import './static/css/iconfont/iconfont.css';
	/*#endif*/
	/*#ifndef MP*/
	@import url('//at.alicdn.com/t/font_1681579_dwilkcq6mvg.css');
	/*#endif*/
	@import './static/css/reset.scss';
	@import './static/css/uni.scss';
</style>
